from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from time import sleep
import os
import datetime
import json


def write_bilibili_video_data(item):
    # print('write_bilibili_video_data', item)
    source_dir = 'video_data'
    target_dir = os.path.join(source_dir, item['videoid'])
    if not os.path.exists(target_dir):
        os.mkdir(target_dir)
    file_name = "{}.txt".format(datetime.datetime.now().strftime("%Y%m%d"))
    file_path = os.path.join(target_dir, file_name)
    with open(file_path, mode='a', encoding='utf8') as file:  # r w rb wb a
        file.write(json.dumps(item, ensure_ascii=False))
        file.write('\n')


def main():
    url = 'https://www.bilibili.com/'
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument("--window-size=3840,2160")
    prefs = {'profile.managed_default_content_settings.images': 2}
    chrome_options.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(chrome_options=chrome_options)
    driver.get(url)

    feed_card = driver.find_elements(By.XPATH, './/div[@class="feed-card"]')

    for card in feed_card:
        item = {}
        item['title'] = card.find_element(By.XPATH, './/h3[@class="bili-video-card__info--tit"]').get_attribute('title')
        item['link'] = card.find_element(By.XPATH, './/h3[@class="bili-video-card__info--tit"]/a').get_attribute(
            'data-target-url')
        item['videoid'] = item['link'].split('/')[-1]
        item['play_time'] = card.find_element(By.XPATH, './/div[@class="bili-video-card__stats--left"]/span[1]/span').text
        item['comments'] = card.find_element(By.XPATH, './/div[@class="bili-video-card__stats--left"]/span[2]/span').text
        item['duration'] = card.find_element(By.XPATH, './/span[@class="bili-video-card__stats__duration"]').text
        item['author'] = card.find_element(By.XPATH, './/span[@class="bili-video-card__info--author"]').get_attribute('title')
        item['date'] = card.find_element(By.XPATH, './/span[@class="bili-video-card__info--date"]').text.replace('· ', '')
        item['datetime'] = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        write_bilibili_video_data(item)
        print(item)
        # break

    sleep(3)

    driver.quit()


if __name__ == "__main__":
    main()
